package domain.entities;

import java.util.Date;
import java.util.List;
import java.util.Set;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

/**
 * A data access object (DAO) providing persistence and search support for
 * Centro entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see domain.entities.Centro
 * @author MyEclipse Persistence Tools
 */

public class CentroDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory.getLogger(CentroDAO.class);
	// property constants
	public static final String BOOK_BA = "bookBa";
	public static final String CORPORATE_NAME = "corporateName";
	public static final String GRYA_SOS_COMMENT = "gryaSosComment";
	public static final String GRYA_SOS_NAME = "gryaSosName";
	public static final String HORARIO_ATENCION = "horarioAtencion";
	public static final String ID_BA = "idBa";
	public static final String INSTITUCION = "institucion";
	public static final String LEGAL_REPRESENTATIVE = "legalRepresentative";
	public static final String PAGE_BA = "pageBa";
	public static final String PSA_CONTACT_INFORMATION = "psaContactInformation";
	public static final String PSA_REFERRED_BY = "psaReferredBy";
	public static final String REQUIRES_IECAPPROVAL = "requiresIecapproval";
	public static final String RUT_CORPORATE = "rutCorporate";
	public static final String RUT_LEGAL_REPRESENTATIVE = "rutLegalRepresentative";

	protected void initDao() {
		// do nothing
	}

	public void save(Centro transientInstance) {
		log.debug("saving Centro instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(Centro persistentInstance) {
		log.debug("deleting Centro instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public Centro findById(java.lang.Integer id) {
		log.debug("getting Centro instance with id: " + id);
		try {
			Centro instance = (Centro) getHibernateTemplate().get(
					"domain.entities.Centro", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List<Centro> findByExample(Centro instance) {
		log.debug("finding Centro instance by example");
		try {
			List<Centro> results = (List<Centro>) getHibernateTemplate()
					.findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Centro instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Centro as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List<Centro> findByBookBa(Object bookBa) {
		return findByProperty(BOOK_BA, bookBa);
	}

	public List<Centro> findByCorporateName(Object corporateName) {
		return findByProperty(CORPORATE_NAME, corporateName);
	}


	public List<Centro> findByGryaSosComment(Object gryaSosComment) {
		return findByProperty(GRYA_SOS_COMMENT, gryaSosComment);
	}

	public List<Centro> findByGryaSosName(Object gryaSosName) {
		return findByProperty(GRYA_SOS_NAME, gryaSosName);
	}

	public List<Centro> findByHorarioAtencion(Object horarioAtencion) {
		return findByProperty(HORARIO_ATENCION, horarioAtencion);
	}

	public List<Centro> findByIdBa(Object idBa) {
		return findByProperty(ID_BA, idBa);
	}

	public List<Centro> findByInstitucion(Object institucion) {
		return findByProperty(INSTITUCION, institucion);
	}

	public List<Centro> findByLegalRepresentative(Object legalRepresentative) {
		return findByProperty(LEGAL_REPRESENTATIVE, legalRepresentative);
	}

	public List<Centro> findByPageBa(Object pageBa) {
		return findByProperty(PAGE_BA, pageBa);
	}

	public List<Centro> findByPsaContactInformation(Object psaContactInformation) {
		return findByProperty(PSA_CONTACT_INFORMATION, psaContactInformation);
	}

	public List<Centro> findByPsaReferredBy(Object psaReferredBy) {
		return findByProperty(PSA_REFERRED_BY, psaReferredBy);
	}

	public List<Centro> findByRequiresIecapproval(Object requiresIecapproval) {
		return findByProperty(REQUIRES_IECAPPROVAL, requiresIecapproval);
	}

	public List<Centro> findByRutCorporate(Object rutCorporate) {
		return findByProperty(RUT_CORPORATE, rutCorporate);
	}

	public List<Centro> findByRutLegalRepresentative(
			Object rutLegalRepresentative) {
		return findByProperty(RUT_LEGAL_REPRESENTATIVE, rutLegalRepresentative);
	}

	public List findAll() {
		log.debug("finding all Centro instances");
		try {
			String queryString = "from Centro";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public Centro merge(Centro detachedInstance) {
		log.debug("merging Centro instance");
		try {
			Centro result = (Centro) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(Centro instance) {
		log.debug("attaching dirty Centro instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(Centro instance) {
		log.debug("attaching clean Centro instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CentroDAO getFromApplicationContext(ApplicationContext ctx) {
		return (CentroDAO) ctx.getBean("CentroDAO");
	}
}